<?php

require('phpMQTT.php');

//  mqtt.serverURIs: tcp://49.234.97.87:1883
//  mqtt.username: admin
//  mqtt.password: seari@RJZX
//  mqtt.client.id: 123
//  mqtt.topic: ECGS

$server = '45.195.57.187';     // change if necessary
$port = 6091;                     // change if necessary
$username = 'admin';                   // set your username
$password = 'public';                   // set your password
$client_id = 'ecgskIriLHTcNXwj9Y2f'; // make sure this is unique for connecting to sever - you could use uniqid()


 	do{
    // echo '测试'.time(); 
	 
	 	 //------------------------------------------------
			$connect = mysqli_connect('118.25.250.93','wohuan','ptCWtcrBtP5NmRLH','wohuan') or die('Unale to connect');
				
				//$buffer = "210811160709MN=1234567PH=00.00;ORP=-2000;Turb=00.00;CL=00.00;Temp=000.0;";    b.cj_time =1630312200
				
		    $sql1 = " select  id,device_code ,now(),topic from iot_node_info where id in (253)";
			$sql12 = " select  id,device_code ,now(),topic from iot_node_info where id in (254)";
 
            $result1 = $connect->query($sql1);
			$result12 = $connect->query($sql12);
            if($result1 === false) {
                die('!数据库连接失败：(' . $connect->connect_errno . ') '. $connect->connect_error);
            }
         
            while($row=$result1->fetch_row()){
                 // echo $row[0].'-'.$row[1].'|'  ; {id:20210000,o2:76.5,ph:8.6,orp:-136,ec:16,tub:0.69,temp:288.5}
				  $sql2 = "select max(a.atime) as tt,b.id,IFNULL(a.sdata, '0') ,b.name,b.measure_unit_type from iot_history_sensor_data a ,iot_sensor_info b where a.sensor_id =b.id and b.node_id=".$row[0]." and a.atime> date_add(now(), interval -1000 minute)  group by b.id  ";
				 $sql3 = "select max(a.atime) as tt,b.id,IFNULL(a.sdata, '0') ,b.name,b.measure_unit_type from iot_history_sensor_data a ,iot_sensor_info b where a.sensor_id =b.id and b.node_id=".254." and a.atime> date_add(now(), interval -1000 minute)    ";
				 $result2 = $connect->query($sql2);
				  $result3 = $connect->query($sql3);
				//  $ret='{"id":'.$row[1].',"ec":-1,"timestamp":"'.date('Y-m-d H:i:s',$row[2]).'",';
				$rowti = str_replace(" ", "T", $row[2]);
				if(mysqli_num_rows($result2)!=0){
			//	    $ret='{"id":"'.$row[1].'","ec":-1,"timestamp":"'.$row[2].'",';
				    while($row2=$result2->fetch_row()){
						while($row3=$result3->fetch_row()){
			//	       $ret =$ret.'"'.$row2[3].'":"'.$row2[2].'",'; 
			          $ret='{"h":{"ID":2,"T":5},"b":{"dvl":[{"da":"Dlysps1","dl":{"流量":'.$row2[2].'}}],{"流量":'.$row3[2].'}}"rf":6,"ts":"'.$rowti.'+08:00"}';
				    }
				    $ret = rtrim ( $ret ,  "," ).'}';
				  
				    echo  '--'.$ret .'\n';
				  
				    //  发送数据
//				     $mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
//				     if ($mqtt->connect(true, NULL, $username, $password)) {
//						 $topic = "ecgs" ;
//						 if($row[3]){
//							$topic = $row[3] ;
//						 }
						 
//				        $mqtt->publish($topic, $ret  , 0, false);
//				     } else {
 //                        echo "Time out!\n";
//                     }
 //                    $mqtt->close();
				}
				
				  //--------------
				  // echo  '0-' ;
				  $result2->close();
                 // echo json_encode( (object)$ret);  "id":"0020210001","name":"PH值","value":6.8
             }
            $result1->close();
	    // -----------------------------------------------------------
	     
   	     
	// $mqtt->publish('ECGS', '{"id":"0020210001","name":"PH值","value":6.8;"id":"0020210001","name":"ORP值","value":-135;"id":"0020210001","name":"电导率值","value":18;"id":"0020210001","name":" 混浊度","value":4;"id":"0020210001","name":"温度","value":76;}' . date('r'), 0, false);
     sleep(10);// 等待5s    
   }while(true);
   
	

